var t = getApp();

Component({
    properties: {
        show: {
            value: !1,
            type: Boolean
        }
    },
    data: {
        addressData: [],
        form: {},
        create: !0,
        popAddAddress: !1,
        addressName: {},
        districtVal: "",
        strShow: !1,
        provinceData: [],
        pickerValue: [ 0, 0, 0 ]
    },
    pageLifetimes: {
        show: function() {
            this.getAddress(), this._initAddressInfo();
        }
    },
    methods: {
        getAddress: function() {
            this.addressData = [];
            var e = this, a = t.getNetAddresss("member.member-address.index");
            t._getNetWork({
                url: a,
                success: function(t) {
                    var a = t.data;
                    if (1 == a.result) {
                        var s = a.data;
                        s = a.data.filter(function(t) {
                            return 1 === t.isdefault;
                        }), e.setData({
                            popupSpecs: !0,
                            addressData: a.data,
                            defaultAddress: s
                        });
                    } else wx.showToast({
                        icon: "none",
                        title: a.msg,
                        duration: 1500
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        },
        goLocation_ro: function() {
            wx.showToast({
                icon: "none",
                title: "error::packageH\newDrawcomponentsyz_addressListyz_addressList.js",
                duration: 1500
            });
        },
        selectAddress: function(t) {
            var e = t.currentTarget.dataset.addr;
            this.triggerEvent("confirm", e);
        },
        editAddress: function(t) {
            console.log(t);
            var e = t.currentTarget.dataset.item;
            this.setData({
                "form.address_id": this.data.defaultAddress[0].id,
                "form.username": this.data.defaultAddress[0].username,
                "form.mobile": this.data.defaultAddress[0].mobile,
                "form.province": this.data.defaultAddress[0].province,
                "form.city": this.data.defaultAddress[0].city,
                "form.district": this.data.defaultAddress[0].district,
                "form.address": this.data.defaultAddress[0].address,
                "form.street": this.data.defaultAddress[0].street,
                "form.isDefault": 0 !== this.data.defaultAddress[0].isDefault,
                "is_point.lng": e.longitude,
                "is_point.lat": e.latitude
            }), this.setData({
                create: !1,
                popAddAddress: !0
            }), this._getStreet(this.data.defaultAddress[0].district_id), this.setData({
                addressName: this.data.form.province + " " + this.data.form.city + " " + this.data.form.district
            });
        },
        popAddressClose: function() {
            this.setData({
                popAddAddress: !1
            });
        },
        usernameinp: function(t) {
            var e = t.detail.value;
            this.setData({
                "form.username": e
            });
        },
        mobileinp: function(t) {
            var e = t.detail.value;
            this.setData({
                "form.mobile": e
            });
        },
        isDefaultChange: function(t) {
            var e = t.detail;
            this.setData({
                "form.isDefault": e
            });
        },
        addressinp: function(t) {
            var e = t.detail.value;
            this.setData({
                "form.address": e
            });
        },
        _closeDateLw: function() {
            this.setData({
                openDateLw: !1
            });
        },
        _initAddressInfo: function() {
            var e = this;
            wx.getStorage({
                key: "provinceData",
                success: function(t) {
                    console.log("地址信息准备就绪"), e._getProvinceData();
                },
                fail: function(a) {
                    console.log("省fail"), t._getNetWork({
                        url: t.getNetAddresss("member.member-address.address"),
                        success: function(t) {
                            var a = t.data, s = a.data.province, i = a.data.city, d = a.data.district;
                            wx.setStorage({
                                key: "provinceData",
                                data: s
                            }), wx.setStorage({
                                key: "cityData",
                                data: i
                            }), wx.setStorage({
                                key: "districtData",
                                data: d
                            }), e._initAddressInfo();
                        },
                        fail: function(t) {},
                        state: !0
                    });
                }
            });
        },
        _getProvinceData: function() {
            var t = this;
            wx.getStorage({
                key: "provinceData",
                success: function(e) {
                    t.setData({
                        provinceData: e.data
                    }), t.setData({
                        "selectAddressData[0].name": e.data[t.data.pickerValue[0]].areaname,
                        "selectAddressData[0].id": e.data[t.data.pickerValue[0]].id,
                        "selectAddressData[0].index": t.data.pickerValue[0]
                    }), t._getCityData();
                }
            });
        },
        _getCityData: function() {
            var t = this.data.selectAddressData[0], e = this;
            wx.getStorage({
                key: "cityData",
                success: function(a) {
                    for (var s = [], i = 0; i < a.data.length; i++) a.data[i].parentid == t.id && s.push(a.data[i]);
                    e.setData({
                        cityData: s
                    }), e.setData({
                        "selectAddressData[1].name": s[e.data.pickerValue[1]].areaname,
                        "selectAddressData[1].id": s[e.data.pickerValue[1]].id,
                        "selectAddressData[1].index": e.data.pickerValue[1]
                    }), e._getDistrictData();
                }
            });
        },
        _getDistrictData: function() {
            var t = this.data.selectAddressData[1], e = this;
            wx.getStorage({
                key: "districtData",
                success: function(a) {
                    console.log("地区读取成功");
                    for (var s = [], i = 0; i < a.data.length; i++) a.data[i].parentid == t.id && s.push(a.data[i]);
                    e.setData({
                        districtData: s
                    }), e.setData({
                        "selectAddressData[2].name": s[e.data.pickerValue[2]].areaname,
                        "selectAddressData[2].id": s[e.data.pickerValue[2]].id,
                        "selectAddressData[2].index": e.data.pickerValue[2]
                    });
                }
            });
        },
        openDateLwtbtn: function() {
            this.setData({
                openDateLw: !0
            });
        },
        bindChange: function(t) {
            var e = t.detail.value;
            this.data.pickerValue[0] != e[0] && (e[1] = 0, e[2] = 0), this.data.pickerValue[1] != e[1] && (e[2] = 0), 
            this.setData({
                "pickerValue[0]": e[0],
                "pickerValue[1]": e[1],
                "pickerValue[2]": e[2]
            }), this._initAddressInfo();
        },
        _okAddress: function() {
            var t = this.data.selectAddressData;
            this.setData({
                "form.street": "",
                districtVal: "",
                addressName: t[0].name + " " + t[1].name + " " + t[2].name,
                "form.province": t[0].name,
                "form.city": t[1].name,
                "form.district": t[2].name
            }), this._getStreet(t[2].id), this._closeDateLw();
        },
        _getStreet: function(e) {
            var a = this, s = t.getNetAddresss("member.memberAddress.street");
            s += "&district_id=" + e, t._getNetWork({
                url: s,
                success: function(e) {
                    var s = e.data;
                    1 != s.result || t._isTextEmpty(s.data) ? a.setData({
                        strShow: !1
                    }) : !t._isTextEmpty(s.data) && s.data.length > 0 ? a.setData({
                        districtVal: s.data,
                        strShow: !0
                    }) : a.setData({
                        strShow: !1
                    });
                },
                fail: function(t) {
                    a.setData({
                        strShow: !1
                    }), console.log(t);
                }
            });
        },
        streetChoose: function() {
            1 == this.data.is_region && t._isTextEmpty(this.data.addressName) ? wx.showToast({
                icon: "none",
                title: "请先选择所在地区",
                duration: 1500
            }) : this.setData({
                streetShow: !0,
                opren: !1
            });
        },
        streetClose: function() {
            this.setData({
                streetShow: !1,
                opren: !0
            });
        },
        streetConfirm: function(t) {
            var e = t.target.dataset.strname;
            this.setData({
                "form.street": e,
                streetShow: !1,
                opren: !0
            });
        },
        appendAddress: function(e) {
            var a = this, s = e.currentTarget.dataset.apptype;
            if (t._isTextEmpty(this.data.form.username)) wx.showToast({
                icon: "none",
                title: "请输入收货人姓名",
                duration: 1500
            }); else if (t._isTextEmpty(this.data.form.mobile)) wx.showToast({
                icon: "none",
                title: "请输入联系电话",
                duration: 1500
            }); else if (/^[0-9]{1,}$/.test(this.data.form.mobile)) if (1 == this.data.is_region && t._isTextEmpty(this.data.addressName)) wx.showToast({
                icon: "none",
                title: "请选择所在区域",
                duration: 1500
            }); else if (1 == this.data.is_region && this.data.strShow && t._isTextEmpty(this.data.form.street)) wx.showToast({
                icon: "none",
                title: "请选择所在街道",
                duration: 1500
            }); else if (t._isTextEmpty(this.data.form.address)) wx.showToast({
                icon: "none",
                title: "请输入详细地址",
                duration: 1500
            }); else {
                var i = {};
                i = {
                    username: this.data.form.username,
                    mobile: this.data.form.mobile,
                    province: this.data.form.province,
                    city: this.data.form.city,
                    district: this.data.form.district,
                    address: this.data.form.address,
                    isdefault: this.data.form.isDefault ? 1 : 0,
                    address_id: this.data.form.address_id
                }, this.data.is_point && (i.longitude = this.data.is_point.lng, i.latitude = this.data.is_point.lat), 
                this.data.strShow && (i.street = this.data.form.street);
                var d = "";
                d = "update" === s ? t.getNetAddresss("member.member-address.update") : t.getNetAddresss("member.member-address.store"), 
                t._postNetWork({
                    url: d,
                    showToastIn: !1,
                    data: i,
                    success: function(t) {
                        var e = t.data;
                        1 == e.result ? (wx.showToast({
                            icon: "none",
                            title: e.msg,
                            duration: 1500
                        }), a.triggerEvent("confirm", a.data.form), a.popAddressClose(), a.setData({
                            "form.username": "",
                            "form.mobile": "",
                            "form.province": "",
                            "form.city": "",
                            "form.district": "",
                            "form.street": "",
                            "form.address": "",
                            "form.isDefault": !1,
                            addressName: ""
                        })) : wx.showToast({
                            icon: "none",
                            title: e.msg,
                            duration: 1500
                        });
                    },
                    fail: function(t) {}
                });
            } else wx.showToast({
                icon: "none",
                title: "请输入联系电话",
                duration: 1500
            });
        },
        onCloseAdd: function() {
            this.setData({
                show: !1
            });
        },
        addAddress: function() {
            this.getIsOpenAddress(), this.showAddAddress();
        },
        getIsOpenAddress: function() {
            var e = this, a = t.getNetAddresss("member.member-address.is-region");
            t._getNetWork({
                url: a,
                success: function(a) {
                    var s = a.data;
                    1 != s.result || t._isTextEmpty(s.data) ? console.log(s.msg) : e.setData({
                        is_region: s.data.is_region
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        },
        showAddAddress: function() {
            this.setData({
                "form.username": "",
                "form.mobile": "",
                "form.province": "",
                "form.city": "",
                "form.district": "",
                "form.street": "",
                "form.address": "",
                "form.isDefault": !1,
                addressName: "",
                popAddAddress: !0,
                create: !0
            });
        }
    }
});